<?php
namespace Admin\Controller;
use Think\Page;

class UserController extends CommonController{
	
	public function allUserInfo(){
		
		$userModel=M('Userinfo');
		//查询总记录数
		$count=$userModel->count();
		//实例化分页类，传入总记录数和每页显示的记录数(4)
		$page=new Page($count,4);
		$page->setConfig('prev','上一页');
		$page->setConfig('next','下一页');
		//总的页码数小于分页类中的rollPage
		$page->rollPage=4;
		$page->lastSuffix = false; // 最后一页是否显示总页数
		$page->setConfig('first','首页');
		$page->setConfig('last','末页');
		//分页显示输出
		$show=$page->show();
		/*
		$sql='SELECT userinfo.*,role.role_name,department.depart_name
			FROM userinfo LEFT JOIN role ON userinfo.user_role_id=role.role_id
	 			LEFT JOIN department ON userinfo.depart_id=department.depart_id;';	
		*/
		//进行分页查询，注意limit方法的参数要使用Page类的属性
		//$data=$userModel->order('depart_id')->limit($page->firstRow.','.$page->listRows)->select();
		$data=$userModel->field('userinfo.*,role.role_name as role_name,department.depart_name as depart_name')
						->join('LEFT JOIN role ON userinfo.user_role_id=role.role_id LEFT JOIN department ON userinfo.depart_id=department.depart_id')
						->limit($page->firstRow.','.$page->listRows)->select();
		//dump($data);
		$this->assign('page',$show);
		$this->assign('data',$data);
		$this->display();
	}

	public function totalUser(){
		$sql='SELECT depart_name,COUNT(*) AS count
			FROM userinfo,department
				WHERE userinfo.depart_id=department.depart_id
					GROUP BY depart_name';
		
		$model= M();
		$data=$model->query($sql);
		/*$data=$model->field("depart_name,count(*) as count")
					->table("userinfo,department")
					->where("userinfo.depart_id=department.depart_id")
					->group("depart_name")
					->select();
					*/
		$str='[';
		foreach ($data as $key => $value) {
			$str.="['".$value['depart_name']."',".$value['count']."],";
		}
		$data=rtrim($str,'').']';
		//dump($str);die();
		$this->assign('data',$data);
		$this->display();
	}
}

?>